<template>
  <div class="app-container">
    <el-card class="box-card">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资料名称" prop="contractFileName">
        <el-input
          v-model="queryParams.contractFileName"
          placeholder="请输入合同资料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资料标识" prop="contractFileId">
        <el-input
          v-model="queryParams.contractFileId"
          placeholder="请输入合同资料标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资料类别" prop="contractFileId" >
        <el-select
          v-model="queryParams.contractFileType"
          placeholder="请选择合同资料类别"
          clearable
        >
          <el-option
            v-for="dict in dict.type.yh_contract_file_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="流水号" prop="sequenceNumber" v-show="showSearch">
          <el-input
            v-model="queryParams.sequenceNumber"
            placeholder="请输入流水号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      <br/>
      <div style="text-align: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
      <br/>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:cjl:data:add']"
        >新增</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:cjl:data:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:cjl:data:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:cjl:data:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="55" :index="indexMethod" align="center" />
     <!-- <el-table-column label="序号" align="center" prop="id" width="60"/>-->
      <el-table-column label="流水号" align="center" prop="sequenceNumber" v-if="columns[0].visible"/>
      <el-table-column label="合同编号" align="center" prop="contractNo" v-if="columns[1].visible"/>
      <el-table-column label="合同资料名称" align="center" prop="contractFileName" v-if="columns[2].visible"/>
      <el-table-column label="合同资料下载" align="center" prop="contractFileUrl" v-if="columns[3].visible">
        <template slot-scope="scope">
        <el-button type="primary"  plain size="mini" @click="downloadEvt(scope.row)">资料下载</el-button>
        </template>
      </el-table-column>
      <el-table-column label="合同资料标识" align="center" prop="contractFileId" v-if="columns[4].visible"/>
      <el-table-column label="合同资料类别" align="center" prop="contractFileType" v-if="columns[5].visible">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.yh_contract_file_type" :value="scope.row.contractFileType"/>
      </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[6].visible"/>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:cjl:data:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:cjl:data:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['module:cjl:data:view']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改合同资料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-container>
          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 20px;">监管任务信息</p>
            </el-divider>
          </el-header>
          <el-main>
        <el-row>
        <el-col :span="8">
        <el-form-item label="流水号" prop="sequenceNumber">
          <el-autocomplete
            clearable
            v-model="form.sequenceNumber"
            placeholder="请输入流水号"
            style="width: 100%"
            :fetch-suggestions="querySearchAsync"
            @select="handleSelect"
          >
          </el-autocomplete>
          <!--<el-input v-model="form.sequenceNumber" placeholder="请输入合同流水号"  style="width: 100%"/>-->
        </el-form-item>
        </el-col>
          <el-col :span="8">
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号"  style="width: 100%" clearable/>
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="监管JS代表室" prop="note1">
              <el-select v-model="form.note1" placeholder="请选择监管JS代表室" style="width: 100%"  clearable>
                <el-option
                  v-for="dict in dict.type.yh_representative_office"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
             <!-- <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true"  placeholder="请选择监管JS代表室" />-->
            </el-form-item>
          </el-col>
        </el-row>
          </el-main>
        </el-container>
        <el-container>
          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 20px;">合同资料信息</p>
            </el-divider>
          </el-header>
          <el-main>
        <el-row>
            <el-col :span="8">
              <el-form-item label="合同资料名称" prop="contractFileName">
                <el-input v-model="form.contractFileName" placeholder="请输入合同资料名称"  style="width: 100%" clearable/>
              </el-form-item>
            </el-col>
          <el-col :span="8">
            <el-form-item label="合同资料标识" prop="contractFileId">
              <el-input v-model="form.contractFileId" placeholder="请输入合同资料标识"  style="width: 100%" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="合同资料类别" prop="contractFileType">
            <el-select v-model="form.contractFileType" placeholder="请选择合同资料类别" style="width: 100%" clearable>
              <el-option
                v-for="dict in dict.type.yh_contract_file_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          </el-col>
        </el-row>
            <el-form-item label="合同资料路径" prop="contractFileUrl">
              <file-upload v-model="form.contractFileUrl" style="width:90%;float:left"/>
              <!--<el-input v-model="form.contractFileUrl" type="textarea" placeholder="请输入内容" />-->
            </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
          </el-main>
        </el-container>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看合同资料信息对话框 -->
    <el-dialog :title="view.title" :visible.sync="view.open" width="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-container>
          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 20px;">合同资料基本信息</p>
            </el-divider>
          </el-header>
          <el-main>
            <el-row>
              <el-col :span="8">
                <el-form-item label="流水号" prop="sequenceNumber">
                  <el-input v-model="form.sequenceNumber"   style="width: 100%" readonly="true"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="合同编号" prop="contractNo">
                  <el-input v-model="form.contractNo"   style="width: 100%" readonly="true"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="监管Js代表室" prop="deptId">
                  <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true"  disabled="true" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="合同资料名称" prop="contractFileName">
                  <el-input v-model="form.contractFileName"   style="width: 100%" readonly="true"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-main>
        </el-container>
        <el-container>
          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 20px;">合同资料上传信息</p>
            </el-divider>
          </el-header>
          <el-main>
            <el-row>
              <el-col :span="8">
                <el-form-item label="合同资料标识" prop="contractFileId">
                  <el-input v-model="form.contractFileId"   style="width: 100%" readonly="true"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="合同资料类别" prop="contractFileType">
                  <el-select v-model="form.contractFileType"  style="width: 100%" disabled="true">
                    <el-option
                      v-for="dict in dict.type.yh_contract_file_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="合同资料路径" prop="contractFileUrl">
              <file-upload v-model="form.contractFileUrl" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" readonly="true" maxlength="500"/>
            </el-form-item>
          </el-main>
        </el-container>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewCancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listFile, getFile, delFile, addFile, updateFile } from "@/api/module/cjl/data";
  import { treeselect } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import watermark from "watermark-dom";
  import { listSearchContract} from "@/api/module/cjl/contract";
  export default {
    name: "File",
    dicts: ['yh_contract_file_type','yh_representative_office'],
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 部门树选项
        deptOptions: undefined,
        // 合同资料表格数据
        fileList: [],
        // 合同台账表格数据
        contractList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          sequenceNumber: null,
          contractNo: null,
          contractFileName: null,
          contractFileUrl: null,
          contractFileId: null,
          contractFileType: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          note1: null,
          note2: null,
          note3: null,
          note4: null,
          note5: null,
          note6: null,
          note7: null,
          note8: null,
          note9: null,
          note10: null,
          orderByColumn:"createTime",
          isAsc:"desc"
        },
        // 表单参数
        form: {},
        view:{
          // 是否显示弹出层（用户查询）
          open: false,
          // 弹出层标题（用户查询）
          title: "",
        },
        // 列信息
        columns: [
          { key: 0, label: `流水号`, visible: true },
          { key: 1, label: `合同编号`, visible: true },
          { key: 2, label: `合同资料名称`, visible: true },
          { key: 3, label: `合同资料标识`, visible: true },
          { key: 4, label: `合同资料路径`, visible: true },
          { key: 5, label: `合同资料类别`, visible: true },
          { key: 6, label: `备注`, visible: false },
        ],
        // 表单校验
        rules: {
          contractFileType: [
            { required: true, message: "合同资料类别不能为空", trigger: "blur" },
          ],
          note1: [
            { required: true, message: "监管Js代表室不能为空", trigger: "blur" },
          ],
        }
      };
    },
    mounted() {
      watermark.load({ watermark_txt: "  "   });
    },
    created() {
      this.getList();
      this.getTreeselect();
      this.getContractInfo();
    },
    methods: {
      /** 查询合同台账列表 */
      getContractInfo() {
        listSearchContract().then(response => {
          this.contractList = response.rows;
        });
      },
      querySearchAsync(queryString, cb) {
        //因为autocomplete只识别value字段,所以用replace(/需要用的字段/g,"value")，将需要用的字段全部替换成value
        //(/***/g 是全部替换，如果不用全部替换，可以参考https://www.w3school.com.cn/jsref/jsref_replace.asp)
        let contractInfoJson= JSON.parse(JSON.stringify(this.contractList).replace(/sequenceNumber/g,"value"));
        let results = queryString ? contractInfoJson.filter(this.createStateFilter(queryString)) : contractInfoJson;
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 0.5*1000);
      },
      //根据输入的字段进行筛选
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toString().toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      //将其他数据自动补全，采用覆盖的方法
      handleSelect(item){
        this.form.sequenceNumber = item.value;
        this.form.contractNo=item.contractNo;
        this.form.note1=item.representativeOffice;
      },
      /** 资料下载 */
      downloadEvt(row,url, fileName = '合同资料文件') {
        const id = row.id || this.ids
        getFile(id).then(response => {
          this.form = response.data;
        const el = document.createElement('a');
        el.style.display = 'none';
        el.setAttribute('target', '_blank');
        fileName && el.setAttribute('download', fileName);
        url=this.$store.state.user.ipaddress+this.form.contractFileUrl
        el.href = url;
        console.log(el);
        document.body.appendChild(el);
        el.click();
        document.body.removeChild(el);
        });
      },
      indexMethod(index) {
        index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
        return index
      },
      /** 查询合同资料列表 */
      getList() {
        this.loading = true;
        listFile(this.queryParams).then(response => {
          this.fileList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 取消按钮
      viewCancel() {
        this.view.open = false;
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          sequenceNumber: null,
          contractNo: null,
          contractFileName: null,
          contractFileUrl: null,
          contractFileId: null,
          contractFileType: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          note1: null,
          note2: null,
          note3: null,
          note4: null,
          note5: null,
          note6: null,
          note7: null,
          note8: null,
          note9: null,
          note10: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.is=selection.map(item => item.sequenceNumber)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加合同资料";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getFile(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改合同资料";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateFile(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFile(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 详细按钮操作 */
      handleView(row) {
       /* const id = row.id || this.ids
        getFile(id).then(response => {
          this.form = response.data;
          this.view.open = true;
          this.view.title = "查看合同资料信息";
        });*/
        const id = row.id;
        this.$router.push({ path:"/hetong/data_detail", query: {id}});
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const i=row.sequenceNumber || this.is;
        this.$modal.confirm('是否确认删除合同流水号为"' + i + '"的数据项？').then(function() {
          return delFile(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('module/cjl/data/export', {
          ...this.queryParams
        }, `file_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style>
  .el-header, .el-footer {
    background-color: #FFFFFF;
    color: #333;
    text-align: left;
    line-height: 60px;
    font-style: oblique;

  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  /*对话框标题样式*/
  .el-dialog__title{
    /*color: #1ab394;*/
    font-weight: bold;
    font-size: 25px;
  }
</style>
